package com.gyw.myapplication.compare;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.MotionEvent;
import android.view.View;


/**
 * Created by Administrator on 2018/12/7.
 */

public class TableRowView extends RecyclerView {
    public TableRowView(Context context) {
        super(context);
    }

    private HorizontalScrollChangedListener listener;

    

    public void setScrollChangedListener(HorizontalScrollChangedListener listener){
        this.listener=listener;
    }


    @Override
    public void onScrolled(int dx, int dy) {
        super.onScrolled(dx, dy);
    }

    public int getScrollDistance() {
        LinearLayoutManager layoutManager = (LinearLayoutManager)getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        if (firstVisiableChildView==null){
            return 0;
        }
        int itemWidth = firstVisiableChildView.getWidth();
        return (position+1) * itemWidth - firstVisiableChildView.getRight();
    }

    public boolean itemAviable() {
        LinearLayoutManager layoutManager = (LinearLayoutManager)getLayoutManager();
        int position = layoutManager.findFirstVisibleItemPosition();
        View firstVisiableChildView = layoutManager.findViewByPosition(position);
        if (firstVisiableChildView==null){
            return false;
        }
        return true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        //记录当前触摸的HListViewScrollView
        if(null!=this.listener){
            listener.setCurrentTouchView(this);
        }
        return super.onTouchEvent(ev);
    }

    public interface HorizontalScrollChangedListener{
        public void setCurrentTouchView(TableRowView currentTouchView);
    }
}
